package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.*;
import com.jian.bus.service.*;
import com.jian.bus.vo.InportVo;
import com.jian.bus.vo.SalesVo;
import com.jian.bus.vo.SalesbackVo;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.WebUtils;
import com.jian.sys.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-26
 */
@RestController
@RequestMapping("/sales")
public class SalesController {
    @Autowired
    CustomerService customerService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    SalesService salesService;
    @Autowired
    SalesbackService salesbackService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllSales")
    public DataGridView loadAllSales(SalesVo salesVo){

        IPage<Sales> page = new Page<>(salesVo.getPage(),salesVo.getLimit());
        QueryWrapper<Sales> wrapper = new QueryWrapper<>();
        wrapper.eq(salesVo.getCustomerid()!=null&&salesVo.getCustomerid()!=0,"customerid",salesVo.getCustomerid());
        wrapper.eq(salesVo.getGoodsid()!=null&&salesVo.getGoodsid()!=0,"goodsid",salesVo.getGoodsid());
        wrapper.like(StringUtils.isNotBlank(salesVo.getOperateperson()),"operateperson",salesVo.getOperateperson());
        wrapper.like(StringUtils.isNotBlank(salesVo.getRemark()),"remark",salesVo.getRemark());
        wrapper.ge(salesVo.getStartTime()!=null,"salestime",salesVo.getStartTime());
        wrapper.le(salesVo.getEndTime()!=null,"salestime",salesVo.getEndTime());
        wrapper.orderByDesc("salestime");
        this.salesService.page(page,wrapper);
        List<Sales> list = page.getRecords();
        //通过商品id设置商品名称 商品规格
        for (Sales sales : list) {
            Goods goods = this.goodsService.getById(sales.getGoodsid());
            if(null!=goods){
                sales.setGoodsname(goods.getGoodsname());
                sales.setSize(goods.getSize());
            }
        }
        //通过客户id设置客户名称
        for (Sales sales : list) {
            Customer customer=this.customerService.getById(sales.getCustomerid());
            if(null!=customer){
                sales.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridView(page.getTotal(),list);
    }
    /**
     * 添加商品销售
     * @param salesVo
     * @return
     */
    @RequestMapping("/addSales")
    public ResultObj addSales(SalesVo salesVo){
        try{
            salesVo.setSalestime(new Date());
            User user= (User) WebUtils.getSession().getAttribute("user");
            salesVo.setOperateperson(user.getName());
            this.salesService.save(salesVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改商品销售
     * @param salesVo
     * @return
     */
    @RequestMapping("/updateSales")
    public ResultObj updateSales(SalesVo salesVo){
        try{
            this.salesService.updateById(salesVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }
    /**
     * 删除商品销售
     * @param id
     * @return
     */
    @RequestMapping("/deleteSales")
    public ResultObj deleteSales(Integer id){
        try {
            this.salesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 退货处理
     * @param id
     * @param remark
     * @param number
     * @return
     */
    @RequestMapping("/addSalesback")
    public ResultObj addSalesback(Integer id,String remark,Integer number){
        try {
            this.salesbackService.saveSalesbackInfo(id,remark,number);
            return ResultObj.OPERATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.OPERATE_ERROR;
        }
    }
}

